package org.tigris.atlas.quizme.service.impl;

import java.lang.Integer;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.tigris.atlas.messages.Message;
import org.tigris.atlas.messages.MessageUtils;
import org.tigris.atlas.quizme.bizobj.QuestionBO;
import org.tigris.atlas.quizme.bizobj.QuizBO;
import org.tigris.atlas.quizme.bizobj.QuizTopicBO;
import org.tigris.atlas.quizme.persist.DaoFactory;
import org.tigris.atlas.quizme.persist.QuestionDao;
import org.tigris.atlas.quizme.persist.UserProfileDao;
import org.tigris.atlas.quizme.service.QuestionCollectionServiceResponse;
import org.tigris.atlas.quizme.service.QuizCollectionServiceResponse;
import org.tigris.atlas.quizme.service.QuizServiceResponse;
import org.tigris.atlas.quizme.service.QuizTopicCollectionServiceResponse;

import org.tigris.atlas.quizme.service.QuizService;
import org.tigris.atlas.quizme.transfer.Answer;
import org.tigris.atlas.quizme.transfer.Question;
import org.tigris.atlas.quizme.transfer.Quiz;
import org.tigris.atlas.quizme.transfer.TransferObjectFactory;
import org.tigris.atlas.quizme.transfer.UserProfile;
import org.tigris.atlas.service.BooleanServiceResponse;

/**
 * Service implementation class for the Quiz service
 * 
 * @author Robert Simmering
 */
public class QuizServiceImpl extends QuizServiceBaseImpl{

	/**
	 * Execute the GetRandomQuestions service
	 *
	 * @return QuestionCollectionServiceResponse
	 */
	public void getRandomQuestionsImpl(String topicId, Integer numQuestions, QuestionCollectionServiceResponse response){
		List questions = QuestionBO.selectQuestionsByTopic(topicId);
		if(questions.size() < numQuestions.intValue()){
			Message msg = MessageUtils.createErrorMessage("errors.tooManyQuestions", null, new String[]{""+questions.size()});
			response.getMessages().addMessage(msg);
		}
		
		List randomQuestions = new ArrayList(numQuestions.intValue());
		if(questions.size() > 0){
			for(int i=0; i < numQuestions.intValue(); i++){		
				Random random = new Random();
				int index = random.nextInt(questions.size());
	
				QuestionBO questionBO = (QuestionBO)questions.get(index);
				questions.remove(index);
				randomQuestions.add(questionBO.getQuestionValues());
			}
		}
		else{
			//TODO throw error
		}
		
		response.addItems(randomQuestions);
	}

	/**
	 * Execute the PrepareQuiz service
	 *
	 * @return QuizServiceResponse
	 */
	public void prepareQuizImpl(Integer numQuestions, UserProfile userProfile, QuizServiceResponse response){
		Quiz quiz = TransferObjectFactory.createQuiz();
		quiz.setNumQuestions(numQuestions);
		quiz.setTimeStarted(new Timestamp(new Date().getTime()));
		quiz.setNumCorrect(new Integer(0));
		quiz.setUserProfile(userProfile);
		response.setQuiz(quiz);
	}
	
	public void selectAllQuizzesForUserImpl(String userProfileId, QuizCollectionServiceResponse response){
		List quizzes = QuizBO.getQuizzesByUserProfileId(userProfileId);
		response.addItems(quizzes);
	}
	
	public void selectAllTopicsImpl(QuizTopicCollectionServiceResponse response){
		List questions = QuizTopicBO.selectAllTopics();
		response.addItems(questions);
	}
	
	public void selectAllQuestionsImpl(QuestionCollectionServiceResponse response){
		List questions = QuestionBO.selectAllQuestions();
		response.addItems(questions);
	}

}